<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$tblname = $_REQUEST["tblname"];
$from = $_REQUEST["from"];
$to = $_REQUEST["to"];

if ($tblname == "")
    $tblname = "all";
$db = NEW myDBS();

if ($from && $to) {
    $and = " AND cheque_prt_date BETWEEN '$from' AND '$to' ";
}

switch ($tblname) {
    case "tbl_pet":
        $sql = "SELECT DISTINCT pet_id, cheque_no, cheque_prt_date, account_no, payment_method, bank_name, branch_name, net "
                . " FROM tbl_pet "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND payment_method <> '' "
                . " $and ";

        break;

    case "tbl_petition":

        $sql = "SELECT DISTINCT p_id AS pet_id, cheque_no, cheque_prt_date, account_no, payment_method, bank_name, branch_name, inpNet AS net "
                . " FROM tbl_petition "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND payment_method <> '' "
                . " $and ";

        break;

    case "tbl_salary":
        $sql = "SELECT pet_id, cheque_no, account_no, cheque_prt_date , payment_method, bank.bank_name, branch.branch_name, sum(salary) AS net "
                . " FROM tbl_salary, tbl_payment, bank, branch "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND tbl_salary.id = tbl_payment.card_id "
                . " AND tbl_payment.bank_id = bank.bank_id "
                . " AND tbl_payment.branch_id = branch.branch_id "
                . " AND payment_method <> '' "
                . " $and "
                . " GROUP BY pet_id,  cheque_no, account_no, payment_method, bank_name, branch_name ";

        break;

    default:
        $sql = "SELECT DISTINCT pet_id, cheque_no, cheque_prt_date, account_no, payment_method, bank_name, branch_name, net "
                . " FROM tbl_pet "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND payment_method <> '' "
                . " $and "
                . " UNION "
                . " SELECT DISTINCT p_id AS pet_id, cheque_no, cheque_prt_date, account_no, payment_method, bank_name, branch_name, inpNet AS net "
                . " FROM tbl_petition "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND payment_method <> '' "
                . " $and "
                . "UNION "
                . "SELECT pet_id, cheque_no, account_no, cheque_prt_date , payment_method, bank.bank_name, branch.branch_name, sum(salary) AS net "
                . " FROM tbl_salary, tbl_payment, bank, branch "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND tbl_salary.id = tbl_payment.card_id "
                . " AND tbl_payment.bank_id = bank.bank_id "
                . " AND tbl_payment.branch_id = branch.branch_id "
                . " AND payment_method <> '' "
                . " $and "
                . " GROUP BY pet_id,  cheque_no, account_no, payment_method, bank_name, branch_name ";

        break;
}
//echo $sql;
$result = $db->runSQL($sql);
?>

<script type="text/javascript">
    $(document).ready(function() {
        $('#petition').dataTable();

        $("#tblname").change(function() {
            //alert($(this).val());
            var tblname = $("#tblname").val();
            var from = $("#from").val();
            var to = $("#to").val();

            if (from != "" && to != "") {
                window.location.href = "index.php?pages=rpt_payment_daily&tblname=" + tblname + "&from=" + from + "&to=" + to;
            } else {
                window.location.href = "index.php?pages=rpt_payment_daily&tblname=" + tblname;
            }
        });

        $("#from").change(function() {
//            alert("adfs");
            var tblname = $("#tblname").val();
            var from = $("#from").val();
            var to = $("#to").val();

            if (from != "" && to != "") {
                window.location.href = "index.php?pages=rpt_payment_daily&tblname=" + tblname + "&from=" + from + "&to=" + to;
            }
        });

        $("#to").change(function() {
//            alert("adfs");
            var tblname = $("#tblname").val();
            var from = $("#from").val();
            var to = $("#to").val();

            if (from != "" && to != "") {
                window.location.href = "index.php?pages=rpt_payment_daily&tblname=" + tblname + "&from=" + from + "&to=" + to;
            }
        });

        $("#from").datepicker({
            dateFormat: "yy-mm-dd",
            //showButtonPanel: true,
            placeholder: "กรุณาเลือกวันที่",
            defaultDate: "+1w",
            changeMonth: true,
            changeYear: true,
            numberOfMonths: 1,
            onClose: function(selectedDate) {
                $("#to").datepicker("option", "minDate", selectedDate);
            }
        });
        $("#to").datepicker({
            dateFormat: "yy-mm-dd",
            //showButtonPanel: true,
            placeholder: "กรุณาเลือกวันที่",
            defaultDate: "+1w",
            changeMonth: true,
            changeYear: true,
            numberOfMonths: 1,
            onClose: function(selectedDate) {
                $("#from").datepicker("option", "maxDate", selectedDate);
            }
        });
    });

</script>

<div class="control-group">
    <div class="controls-row">
        <input type="text" id="from" name="from" placeholder="กรุณาเลือกวันที่เริ่ม" value="<?php echo $from; ?>" />
        <input type="text" id="to" name="to" placeholder="กรุณาเลือกวันที่สิ้นสุด" value="<?php echo $to; ?>" />
        <select id="tblname" name="tblname" >
            <option value="">เลือก...</option>
            <option value="all" <?php echo ($tblname == "all") ? "selected" : ""; ?>>ทั้งหมด</option>
            <option value="tbl_pet" <?php echo ($tblname == "tbl_pet") ? "selected" : ""; ?>>อื่นๆ</option>
            <option value="tbl_petition" <?php echo ($tblname == "tbl_petition") ? "selected" : ""; ?>>โครงการ</option>
            <option value="tbl_salary" <?php echo ($tblname == "tbl_salary") ? "selected" : ""; ?>>เงินเดือน</option>
        </select>
        <!--<a href="javascript:printCheque()" class=" btn btn-primary" target="_blank"><i class="icon-print icon-white"></i> บันทึกเช็ค</a>-->
    </div>
</div>

<table id="petition" class="table table-bordered table-hover" width="100%" >
    <caption><br></caption>
    <thead>
        <tr>
            <th>#</th>
            <th>เลขที่ฎีกา</th>
            <th>การจ่ายเงิน</th>
            <th>เลขที่เช็ค / เลขที่บัญชี</th>
            <th>วันที่</th>
            <th>ธนาคาร</th>
            <th>สาขา</th>
            <th>จำนวนเงิน</th>

        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        while ($rows = $db->fetch_object($result)) {
            ?>
            <tr>
                <td>&nbsp;<?php echo $i; ?></td>
                <td>&nbsp;<?php echo $rows->pet_id; ?></td>
                <td>&nbsp;<?php echo $rows->payment_method; ?></td>
                <td>&nbsp;
                    <?php
                    switch ($rows->payment_method) {
                        case "cheque":
                            echo $rows->cheque_no;

                            break;

                        case "transfer":
                            echo $rows->account_no;

                            break;
                        case "trans":
                            echo $rows->account_no;

                            break;
                        default:
                            break;
                    }
                    ?>
                </td>

                <td>&nbsp;<?php echo $rows->cheque_prt_date; ?></td>
                <td>&nbsp;<?php echo $rows->bank_name; ?></td>
                <td>&nbsp;<?php echo $rows->branch_name; ?></td>
                <td><p class="text-right">&nbsp;<?php echo number_format($rows->net, 2); ?></p></td>
            </tr>
            <?php
            $i++;
        }
        ?>
    </tbody>
</table>

